package com.easonji.admin.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.easonji.admin.manager.UserManager;
import com.easonji.admin.mapper.LoginLogMapper;
import com.easonji.core.util.ContrastUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.easonji.admin.model.LoginLog;
import com.easonji.admin.query.LoginLogQuery;
import com.easonji.admin.service.LoginLogService;
import com.easonji.common.base.service.AbstractBaseService;
import com.easonji.common.factory.PageFactory;
import com.easonji.common.page.PageInfo;
import com.easonji.common.util.JsonUtils;
import com.easonji.common.util.StringUtils;

/**
 * @ClassName: LoginLogService
 * @Description: 登录日志服务实现类
 * @Author: LiuJun
 * @date: 2018/07/30 16:11:36
 * @Version: 1.0
 * @Copyright: Copyright (c) 2018 - 2019
 */
@Service
public class LoginLogServiceImpl extends AbstractBaseService<LoginLog> implements LoginLogService {

    @Resource
    private LoginLogMapper loginLogMapper;

    @Resource
    private UserManager userManager;

    @Override
    public Object queryLoginLogList(LoginLogQuery loginLogQuery) {
        // 分页信息
        Page<LoginLog> page = new PageFactory<LoginLog>().defaultPage();
        // 查询条件
        Wrapper<LoginLog> wrapper = new EntityWrapper<>();
        String beginTime = loginLogQuery.getBeginTime();
        if (StringUtils.isNotEmpty(beginTime)) {
            wrapper.ge("gmt_create", beginTime);
        }
        String endTime = loginLogQuery.getEndTime();
        if (StringUtils.isNotEmpty(endTime)) {
            wrapper.le("gmt_create", endTime);
        }
        String condition = loginLogQuery.getCondition();
        if (StringUtils.isNotEmpty(condition)) {
            wrapper.like("log_name", condition);
        }
        // 排序
        wrapper.orderBy("gmt_create");
        List<LoginLog> loginLogs = loginLogMapper.selectPage(page, wrapper);
        return new PageInfo(warp(loginLogs), page.getTotal());
    }

    @Override
    public Boolean removeLoginLog() {
        Wrapper<LoginLog> wrapper = new EntityWrapper<>();
        wrapper.ge("id", 0);
        return loginLogMapper.delete(wrapper) > 0 ? true : false;
    }

    @Override
    protected JSONObject warp2Json(LoginLog loginLog) {
        JSONObject result = JsonUtils.parseObject(loginLog);
        result.put("userName", userManager.getUserNameById(loginLog.getUserId()));
        String message = loginLog.getMessage();
        // 如果信息过长,则只截取前100位字符串
        int msgMaxLength = 100;
        if (StringUtils.isNotEmpty(message) && message.length() >= msgMaxLength) {
            String subMessage = message.substring(0, 100) + "...";
            result.put("message", subMessage);
        }
        // 如果信息中包含分割符号;;; 则分割字符串返给前台
        if (StringUtils.isNotEmpty(message) && message.indexOf(ContrastUtils.SEPARATOR) != -1) {
            String[] msgs = message.split(ContrastUtils.SEPARATOR);
            result.put("regularMessage", msgs.toString());
        } else {
            result.put("regularMessage", message);
        }
        return result;
    }
}
